package sale.wxb.loveshopping.utils;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sale.wxb.loveshopping.entity.common.ResultException;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;

@Component
@Data
public class EMailUtils {
    @Value("${spring.mail.username}")
    private String emailAccount;
    @Value("${spring.mail.verifyCodeSubject}")
    private String emailSubject;
    @Value("${spring.mail.from}")
    private String emailFrom;

    @Resource
    private JavaMailSenderImpl javaMailSender;
    /**
     * 邮件模板路径
     */
    private final ClassPathResource classPathResource;
    private final String template;

    EMailUtils() throws IOException {
        classPathResource = new ClassPathResource("template/email/email-tp.html");
        File file = classPathResource.getFile();

        if (!file.exists()) {
            throw new ResultException("读取邮件模板失败，文件不存在! " + classPathResource.getPath());
        }

        template = new String(Files.readAllBytes(file.toPath()));

        if (!StringUtils.hasText(template)) {
            throw new ResultException("邮件模板内容为空!" + classPathResource.getPath());
        }
    }

    /**
     * 发送邮件
     * @param email 邮箱
     * @param subject 主题
     * @param message 要发送的内容
     */
    public void send(String email, String subject, String message) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mailMessage = new MimeMessageHelper(mimeMessage, true);
            mailMessage.setFrom(emailAccount);
            mailMessage.setTo(email);
            mailMessage.setSubject(subject);
            mailMessage.setText(message, true);
            javaMailSender.send(mimeMessage);
        } catch (Exception ex) {
            throw  new ResultException("发送失败 " + ex.getMessage());
        }
    }

    /**
     * 发送邮件
     * @param email 邮箱
     * @param message 要发送的内容
     */
    public void send(String email, String message) {
        send(email, emailSubject, message);
    }

    /**
     * 格式化验证码模板
     * @return String
     */
    public String formatterVerifyCodeTemplateContent(String code) {
        StringBuilder codes = new StringBuilder();
        for (String str : code.split("")) {
            codes.append("<button class=\"button\">").append(str).append("</button>");
        }
        String tp = template;
        tp = tp.replace("$verifyCode", codes.toString());
        tp = tp.replace("$sendFrom", emailFrom);
        return tp;
    }
}
